home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / basics / glwidget2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.8 KB  |  306 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *
  19.  *  glwidget2.c - an example of a GL widget and a work procedure used
  20.  *                with the Motif toolkit.
  21.  *
  22.  *  Ivan Hajadi
  23.  *  12-June-1992
  24.  *
  25.  *  to compile:
  26.  *  cc -o glwidget2 glwidget2.c -lXirisw -lgl_s -lXm -lXt -lX11_s -lPW -lsun 
  27.  */
  28.  
  29. /* Motif includes */
  30. #include <Xm/Xm.h>
  31. #include <Xm/Form.h>
  32. #include <Xm/PushB.h>
  33.  
  34. /* GL widget include */
  35. #include <X11/Xirisw/GlxMDraw.h>
  36.  
  37.  
  38. GLXconfig rgb_mode[] = {
  39.     { GLX_NORMAL, GLX_RGB, TRUE },
  40.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  41.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  42.     { 0,          0,       0,   }
  43. };
  44.  
  45. XtAppContext app_context;
  46. XtWorkProcId work_procid = 0;
  47. int angle_x = 0, angle_y = 0;
  48.  
  49. void drawscene(int, int);
  50.  
  51. main(int argc, char** argv)
  52. {
  53.     Widget top, form, quit_but, anim_but;
  54.     Widget gl_widget;
  55.  
  56.     /* Prototypes */
  57.     void installColormap(Widget toplevel, Widget glw);
  58.     void quitCB(Widget, XtPointer, XtPointer);
  59.     void ginitCB(Widget, XtPointer, XtPointer);
  60.     void exposeCB(Widget, XtPointer, XtPointer);
  61.     void animateCB(Widget, XtPointer, XtPointer);
  62.  
  63.     Arg args[20];
  64.     int n;
  65.     
  66.     top = XtAppInitialize(&app_context, "Cube", 
  67.         NULL, 0, &argc, argv, NULL, NULL, 0
  68.     );
  69.     n=0;
  70.     XtSetArg(args[n], XmNwidth, 730); n++;
  71.     XtSetArg(args[n], XmNheight, 445); n++;
  72.     form = XmCreateForm(top, "form", args, n);
  73.     XtManageChild(form);
  74.     
  75.     /* Create GL widget */
  76.     
  77.     n = 0;
  78.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  79.     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  80.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++; 
  81.     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  82.     XtSetArg(args[n], XmNleftOffset, 20); n++;
  83.     XtSetArg(args[n], XmNrightOffset, 120); n++;
  84.     XtSetArg(args[n], XmNbottomOffset, 40); n++;
  85.     XtSetArg(args[n], XmNtopOffset, 40); n++;
  86.     XtSetArg(args[n], XmNwidth, 500); n++;
  87.     XtSetArg(args[n], XmNheight, 400); n++;
  88.     XtSetArg(args[n], GlxNglxConfig, rgb_mode); n++;
  89.     gl_widget = GlxCreateMDraw(form, "gl_widget", args, n);
  90.     XtManageChild(gl_widget);
  91.     
  92.     /* Add callbacks */
  93.     
  94.     XtAddCallback(gl_widget, GlxNginitCallback, ginitCB, NULL);
  95.     XtAddCallback(gl_widget, GlxNexposeCallback, exposeCB, NULL);
  96.  
  97.     n=0;
  98.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  99.     XtSetArg(args[n], XmNrightOffset, 10); n++;
  100.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  101.     XtSetArg(args[n], XmNbottomOffset, 10); n++;
  102.     XtSetArg(args[n], XmNtraversalOn, False); n++;
  103.     quit_but = XmCreatePushButton(form, " Quit ", args, n);
  104.     XtManageChild(quit_but);
  105.  
  106.     n=0;
  107.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  108.     XtSetArg(args[n], XmNrightOffset, 10); n++;
  109.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  110.     XtSetArg(args[n], XmNbottomOffset, 50); n++;
  111.     XtSetArg(args[n], XmNtraversalOn, False); n++;
  112.     anim_but = XmCreatePushButton(form, "Rotate", args, n);
  113.     XtManageChild(anim_but);
  114.     
  115.     XtAddCallback(quit_but, XmNactivateCallback, quitCB, NULL);
  116.     XtAddCallback(anim_but, XmNactivateCallback, animateCB, NULL);
  117.     
  118.     XtRealizeWidget(top);
  119.     installColormap(top, gl_widget);
  120.     XtAppMainLoop(app_context);
  121. }
  122.  
  123. /*
  124.  * This routine will install a particular gl widgets's colormap onto the
  125.  * top level window.  It may not be called until after the windows have
  126.  * been realized.
  127.  */
  128. void installColormap(Widget toplevel, Widget glw)
  129. {
  130.     Window windows[2];
  131.  
  132.     windows[0] = XtWindow(glw);
  133.     windows[1] = XtWindow(toplevel);
  134.     XSetWMColormapWindows(XtDisplay(toplevel), XtWindow(toplevel), windows, 2);
  135. }
  136.  
  137. Boolean animate(XtPointer clientD)
  138. {
  139.     angle_y += 75;
  140.     angle_x += 75;
  141.     
  142.     if ((angle_y >= 3600) || (angle_y <= -3600))
  143.         angle_y = 0;
  144.     if ((angle_x >= 3600) || (angle_x <= -3600))
  145.         angle_x = 0;
  146.  
  147.     drawscene(angle_x, angle_y);
  148.  
  149.     /* 
  150.        Return False so this WorkProc 
  151.        keeps getting called. 
  152.     */
  153.     return False;
  154. }
  155.  
  156. void animateCB(Widget w, XtPointer clientD, XtPointer callD)
  157. {
  158.     static Boolean started = False;
  159.     Arg args[1];
  160.     XmString xms;
  161.  
  162.     if (! started ) {
  163.         Boolean animate(XtPointer);
  164.         work_procid = XtAppAddWorkProc(app_context, animate, NULL);
  165.         started = True;
  166.         xms = XmStringCreateSimple(" Stop ");
  167.     } else {
  168.         XtRemoveWorkProc(work_procid);
  169.         started = False;
  170.         xms = XmStringCreateSimple("Rotate");
  171.     }
  172.  
  173.     /* Changed the button label */
  174.     XtSetArg(args[0], XmNlabelString, xms);
  175.     XtSetValues(w, args, 1);
  176.     XmStringFree(xms);
  177. }
  178.  
  179. void ginitCB(Widget w, XtPointer clientD, XtPointer callD)
  180. {
  181.     GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
  182.  
  183.     GLXwinset(XtDisplay(w), call_data->window);
  184.     
  185.     shademodel(GOURAUD);
  186.     zbuffer(TRUE);
  187.     subpixel(TRUE);
  188.     lsetdepth(getgdesc(GD_ZMIN), getgdesc(GD_ZMAX));
  189.     mmode(MVIEWING);
  190.     perspective(450, 1, 1, 100);
  191. }    
  192.  
  193. void exposeCB(Widget w, XtPointer clientD, XtPointer callD)
  194. {
  195.     GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
  196.  
  197.     GLXwinset(XtDisplay(w), call_data->window);
  198.     drawscene(angle_x, angle_y);
  199. }
  200.  
  201. void quitCB(Widget w, XtPointer clientD, XtPointer callD)
  202. {
  203.     exit(0);
  204. }
  205.  
  206. void drawscene(int angle_x, int angle_y)
  207. {
  208.     /* Draw smooth-shaded cube */
  209.     static long v1[4][3] = {
  210.         {-10, -10, 10},
  211.         {10, -10, 10},
  212.         {10, 10, 10},
  213.         {-10, 10, 10},
  214.     };
  215.     static long v2[4][3] = {
  216.         {-10, -10, -10},
  217.         {-10, 10, -10},
  218.         {10, 10, -10},
  219.         {10, -10, -10},
  220.     };
  221.     static long colors1[] = {
  222.         0x000000FF,
  223.         0x00FF0000,
  224.         0x0000FF00,
  225.         0x00FFFF00,
  226.     };
  227.     static long colors2[] = {
  228.         0x0000FFFF,
  229.         0x00FF00FF,
  230.         0x00FFFFFF,
  231.         0x0FF00000,
  232.     };
  233.     
  234.     register int i;
  235.     
  236.     reshapeviewport();
  237.     czclear(0x00777777, getgdesc(GD_ZMAX));
  238.     pushmatrix();
  239.     polarview(80, 0, 250, 0);
  240.     
  241.     pushmatrix();
  242.     rotate(angle_x, 'x');
  243.     rotate(angle_y, 'y');
  244.     
  245.     bgnpolygon();
  246.         for(i=0; i<4; i++) {
  247.             cpack(colors1[i]);
  248.             v3i(v1[i]);
  249.         }
  250.     endpolygon();
  251.     bgnpolygon();
  252.         for(i=0; i<4; i++) {
  253.             cpack(colors2[i]);
  254.             v3i(v2[i]);
  255.         }
  256.     endpolygon();
  257.     
  258.     bgnpolygon();
  259.         cpack(colors1[1]);
  260.         v3i(v1[1]);
  261.         cpack(colors1[2]);
  262.         v3i(v1[2]);
  263.         cpack(colors2[2]);
  264.         v3i(v2[2]);
  265.         cpack(colors2[3]);
  266.         v3i(v2[3]);
  267.     endpolygon();
  268.     bgnpolygon();
  269.         cpack(colors1[0]);
  270.         v3i(v1[0]);
  271.         cpack(colors1[3]);
  272.         v3i(v1[3]);
  273.         cpack(colors2[1]);
  274.         v3i(v2[1]);
  275.         cpack(colors2[0]);
  276.         v3i(v2[0]);
  277.     endpolygon();
  278.  
  279.     bgnpolygon();
  280.         cpack(colors1[2]);
  281.         v3i(v1[2]);
  282.         cpack(colors1[3]);
  283.         v3i(v1[3]);
  284.         cpack(colors2[1]);
  285.         v3i(v2[1]);
  286.         cpack(colors2[2]);
  287.         v3i(v2[2]);
  288.     endpolygon();
  289.     bgnpolygon();
  290.         cpack(colors1[1]);
  291.         v3i(v1[1]);
  292.         cpack(colors1[0]);
  293.         v3i(v1[0]);
  294.         cpack(colors2[0]);
  295.         v3i(v2[0]);
  296.         cpack(colors2[3]);
  297.         v3i(v2[3]);
  298.     endpolygon();
  299.  
  300.     popmatrix();
  301.     popmatrix();
  302.     
  303.     swapbuffers();
  304. }
  305.  
  306.